﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;


namespace DataBaseTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string server = textBox1.Text;
            string database =textBox5.Text;
            string user = textBox2.Text;
            string pass = textBox3.Text;
            string ext = textBox4.Text;
            

            


            int index=tabControl1.SelectedIndex;


            switch (index)
            {
                case 0:
                    if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database) || string.IsNullOrEmpty(user))
                    {
                        MessageBox.Show("server,database,user can not be empty");
                        return;
                    }
                    test_SQLSERVER(server, database, user, pass, ext);
                    break;
                case 1:
                    if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database) || string.IsNullOrEmpty(user))
                    {
                        MessageBox.Show("server,database,user can not be empty");
                        return;
                    }
                    test_MYSQL(server, database, user, pass);
                    break;
                case 2:
                    if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database))
                    {
                        MessageBox.Show("server,database can not be empty");
                        return;
                    }

                    test_SQLITE(server, database,pass);
                    break;

                case 3:

                    test_Odbc(server, database, pass);

                    break;
            }

         


        }

        private void test_Odbc(string server, string database,string pass) {

            try
            {
                //DSN:mylink数据源的名称 UID:sql server登录时的身份sa PWD:登录时的密码123456
                //生成连接数据库字符串
                string ConStr = "DSN=joptshop;UID=dba;PWD=sql";
                //定义SqlConnection对象实例
                OdbcConnection odbcCon = new OdbcConnection(ConStr);
                string SqlStr = "select * from StudentInfo";

                OdbcDataAdapter odbcAdapter = new OdbcDataAdapter(SqlStr, odbcCon);
                DataSet ds = new DataSet();

                odbcAdapter.Fill(ds);
                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void test_SQLSERVER(string server,string database,string user,string pass,string ext)
        {
            string connectionStringTest = @"server=" + server + ";database=" + database + ";user id=" + user + ";password=" + pass + ";" + ext;
            SqlConnection conn = new SqlConnection(connectionStringTest);

            try
            {
                conn.Open();
                MessageBox.Show("connection successfull");
            }
            catch
            {
                MessageBox.Show("connection failed");
            }

            try
            {
                conn.Close();
                conn.Dispose();
            }
            catch
            {

            }
            
        }

        private void test_MYSQL(string server, string database, string user, string pass)
        {
            
            string ConnectString = string.Format("Server={0};User ID={1};Password={2};Database={3};SslMode = none;", server, user, pass, database);
            MySqlConnection conn = new MySqlConnection(ConnectString);

           
            try
            {
                conn.Open();
                MessageBox.Show("connection successfull");
            }
            catch
            {
                MessageBox.Show("connection failed");
            }

            try
            {
                conn.Close();
                conn.Dispose();
            }
            catch
            {

            }

        }


        private void test_SQLITE(string server, string database, string pass)
        {

            string p = "";
            if (!string.IsNullOrEmpty(pass))
            {
                p = "Password=" + pass;
            }

            string ConnectionString = "Data Source="+server+";Version=3;"+p;

            SQLiteConnection conn = new SQLiteConnection(ConnectionString);

            
            try
            {
                conn.Open();
                MessageBox.Show("connection successfull");
            }
            catch
            {
                MessageBox.Show("connection failed");
            }

            try
            {
                conn.Close();
                conn.Dispose();
            }
            catch
            {

            }

        }







        private void button2_Click(object sender, EventArgs e)
        {


            

            string server = textBox1.Text;
            string database = textBox5.Text;
            string user = textBox2.Text;
            string pass = textBox3.Text;
            string ext = textBox4.Text;
            string namespace_str = textBox6.Text;


           


            int index = tabControl1.SelectedIndex;


            switch (index)
            {
                case 0:
                    if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database) || string.IsNullOrEmpty(user) || string.IsNullOrEmpty(namespace_str))
                    {
                        MessageBox.Show("server,database,user,namespace can not be empty");
                        return;
                    }
                    Generate_SQLServer(server, database, user, pass, ext, namespace_str);
                    MessageBox.Show("success");
                    System.Diagnostics.Process.Start(Application.StartupPath + "//entity");
                    break;
                case 1:
                    if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database) || string.IsNullOrEmpty(user) || string.IsNullOrEmpty(namespace_str))
                    {
                        MessageBox.Show("server,database,user,namespace can not be empty");
                        return;
                    }
                    Generate_MYSQL(server, database, user, pass,namespace_str);
                    MessageBox.Show("success");
                    System.Diagnostics.Process.Start(Application.StartupPath + "//entity");
                    break;
                case 2:
                    if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database)  || string.IsNullOrEmpty(namespace_str))
                    {
                        MessageBox.Show("server,database,namespace can not be empty");
                        return;
                    }
                    Generate_SQLITE(server, database, pass, namespace_str);
                    MessageBox.Show("success");
                    System.Diagnostics.Process.Start(Application.StartupPath + "//entity");
                    break;

            }

           


        }



        private void Generate_SQLServer(string server,string database,string user,string pass,string ext,string namespace_str)
        {

            string connectionStringTest = @"server=" + server + ";database=" + database + ";user id=" + user + ";password=" + pass + ";" + ext;

            SqlConnection con = getConnection(connectionStringTest);

            string path = Application.StartupPath + "//entity";

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }


            string path2 = Application.StartupPath + "//dao";

            if (Directory.Exists(path2))
            {
                Directory.Delete(path2, true);
            }


            try
            {

                //select TABLE_NAME,TABLE_TYPE,TABLE_CATALOG,TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES;


                string sql = "select TABLE_NAME,TABLE_TYPE,TABLE_CATALOG,TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE';";

                DataTable dt = Select(sql, con);


                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    string tb_name = dt.Rows[i]["TABLE_NAME"].ToString();
                    // Console.WriteLine("表名称:"+tb_name);
                    label7.Text = tb_name;
                    string head = "using System.Data; \r\n\r\n\r\n";
                    head += "using System.Collections.Generic;\r\n";
                    head += "namespace " + namespace_str + " \r\n  {\r\n   class " + tb_name + " \r\n { \r\n";
                    head += "public " + tb_name + " () \r\n { \r\n }\r\n ";
                    // Console.WriteLine(head);

                    sql = "select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE from INFORMATION_SCHEMA.COLUMNS t where t.TABLE_NAME = '" + tb_name + "';";

                    DataTable dt2 = Select(sql, con);

                    sql = "SELECT OBJECT_NAME(sysobjects.parent_obj) tableName, syscolumns.name AS columnName FROM sysobjects INNER JOIN sysconstraints ON sysobjects.xtype in('PK')   AND sysobjects.id = sysconstraints.constid inner JOIN syscolumns ON sysconstraints.id = syscolumns.id ";

                    DataTable dt3 = Select(sql, con);

                    string pk = "";

                    if (dt3.Rows.Count > 0)
                    {
                        DataRow[] r = dt3.Select("tableName='" + tb_name + "'");
                        if (r.Length > 0)
                        {
                            pk = r[0]["columnName"].ToString();
                        }
                    }

                    string code_str = "";

                    string fields = "";

                    string sql_insert = "";

                    string sql_update = "";

                    string sql_delete = "";

                    string save_parsm_list = "";

                    string update_parsm_list = "";

                    string delete_parsm_list = "";




                    sql_insert += "insert into " + tb_name + " ( ";

                    sql_update += "update " + tb_name + " set ";

                    sql_delete += " delete from " + tb_name;

                    string sql_insert_params_key = "";

                    string sql_insert_params_value = "";


                    string sql_update_params_key = "";

                    string sql_update_where = "";



                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {

                        string col_name = dt2.Rows[j]["COLUMN_NAME"].ToString();

                        string col_type = dt2.Rows[j]["DATA_TYPE"].ToString();

                        SqlDbType dbt = SQLServerUtils.SqlTypeString2SqlType(col_type);



                        Type t = SQLServerUtils.SqlType2CsharpType(dbt);



                        code_str += "\t public\t" + t.ToString() + "\t" + col_name.ToLower() + " {get;set;}\r\n";

                        fields += " dict.Add(\"" + col_name.ToLower() + "\", \"" + col_type + "\");\r\n";

                        if (!string.IsNullOrEmpty(pk))
                        {
                            if (col_name.Equals(pk))
                            {
                                delete_parsm_list = " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";

                                sql_update_where += col_name.ToLower() + "=@" + col_name.ToLower();

                            }
                            else
                            {
                                sql_insert_params_key += "" + col_name.ToLower() + ",";
                                sql_insert_params_value += "@" + col_name.ToLower() + ",";

                                sql_update_params_key += "" + col_name.ToLower() + "=@" + col_name.ToLower() + ",";

                                save_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";
                            }
                        }
                        else
                        {
                            save_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";
                        }


                        update_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";



                        // Console.WriteLine(code_str);


                    }

                    if (!string.IsNullOrEmpty(fields))
                    {
                        fields = fields.Substring(0, fields.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_insert_params_key))
                    {
                        sql_insert_params_key = sql_insert_params_key.Substring(0, sql_insert_params_key.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_insert_params_value))
                    {
                        sql_insert_params_value = sql_insert_params_value.Substring(0, sql_insert_params_value.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_update_params_key))
                    {
                        sql_update_params_key = sql_update_params_key.Substring(0, sql_update_params_key.Length - 1);
                    }



                    sql_insert += sql_insert_params_key + " ) values (" + sql_insert_params_value + ") ; ";

                    sql_update += sql_update_params_key + " where " + sql_update_where + ";";

                    sql_delete += " where " + sql_update_where;


                    string fields_str = "\r\n public Dictionary<string,string> fields(){\r\n";
                    fields_str += "Dictionary<string, string> dict = new Dictionary<string, string>();\r\n ";
                    fields_str += fields;
                    fields_str += "return dict;";
                    fields_str += "}\r\n";

                    string foot = "\r\n}\r\n}";
                    // Console.WriteLine(foot);

                    string full_text = head + code_str + fields_str + foot;


                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch
                    {

                    }

                    string file = path + "//" + tb_name + ".cs";

                    FileStream fs = new FileStream(file, FileMode.Create);
                    //获得字节数组
                    byte[] data = Encoding.Default.GetBytes(full_text);
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();



                    //创建操作类

                    string param = tb_name.ToLower();

                    full_text = "";

                    head = "using System; \r\n using System.Data; \r\n\r\n\r\n";
                    head += "using System.Data.SqlClient;\r\n";
                    head += "namespace " + namespace_str + " \r\n  {\r\n   class " + tb_name + "_DAO \r\n { \r\n";
                    head += "public " + tb_name + "_DAO () \r\n { \r\n }\r\n ";

                    full_text += head;

                    full_text += " SuperDAO super = new SuperDAO(); \r\n";

                    full_text += "\r\n public int save(" + tb_name + " " + param + ")\r\n{\r\n ";

                    full_text += " SqlConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_insert + "\"; \r\n";

                    full_text += "SqlCommand cmd = new SqlCommand(sql, con);\r\n";

                    full_text += save_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += " cmd = new SqlCommand(\"select @@IDENTITY as 'id'\", con); \r\n"; ;
                    full_text += "int id = Convert.ToInt32(cmd.ExecuteScalar());\r\n";
                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";
                    full_text += "return id; \r\n";

                    full_text += "  }\r\n";

                    full_text += "\r\n public void update(" + tb_name + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " SqlConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_update + "\"; \r\n";

                    full_text += "SqlCommand cmd = new SqlCommand(sql, con);\r\n";

                    full_text += update_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";

                    full_text += "  }\r\n";

                    full_text += "\r\n public void delete(" + tb_name + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " SqlConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_delete + "\"; \r\n";

                    full_text += "SqlCommand cmd = new SqlCommand(sql, con);\r\n";

                    full_text += delete_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";

                    full_text += "  }\r\n";

                   

                    full_text += "\r\n}\r\n}";


                    try
                    {
                        Directory.CreateDirectory(path2);
                    }
                    catch
                    {

                    }


                    file = path2 + "//" + tb_name + "_DAO.cs";

                    fs = new FileStream(file, FileMode.Create);
                    //获得字节数组
                    data = Encoding.Default.GetBytes(full_text);
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();






                }


                string file3 = path2 + "//SuperDAO.cs";

                FileStream fs3 = new FileStream(file3, FileMode.Create);
                //获得字节数组
                string sd_text = "";

                sd_text += "using System;\r\n";

                sd_text += "using System.Data; \r\n";

                sd_text += "using System.Data.SqlClient; \r\n";

                sd_text += "using System.Windows.Forms; \r\n";

                sd_text += "namespace " + namespace_str + " { \r\n";

                sd_text += "  class SuperDAO {\r\n ";

                sd_text += "  public SuperDAO() {\r\n  } \r\n";

                sd_text += " public SqlConnection getConnection() { \r\n ";

                sd_text += " string server = \"" + server + "\"; \r\n";

                sd_text += "  string database = \"" + database + "\"; \r\n";
                sd_text += "string user_id = \"" + user + "\"; \r\n";
                sd_text += "  string password = \"" + pass + "\" ; \r\n";
                sd_text += " string Connection_Timeout = \"10\"; \r\n";

                sd_text += " string connectionStringTest = @\"server = \" + server + \"; database = \" + database + \"; user id = \" + user_id + \"; password = \" + password + \"; Connection Timeout = \" + Connection_Timeout + \"; \"; \r\n";

                sd_text += "return new SqlConnection(connectionStringTest); \r\n";

                sd_text += " } \r\n";


                sd_text += "public DataTable getData(string tableName, string where) { \r\n";

                sd_text += "  string sql = \"select* from \" + tableName; \r\n";

                sd_text += " if (!string.IsNullOrEmpty(where)) {  \r\n";

                sd_text += " sql += \" where \" + where;  \r\n";

                sd_text += " } \r\n";

                sd_text += "  DataTable dataTable = Select(sql); \r\n";

                sd_text += " return dataTable;  \r\n  } \r\n ";

                sd_text += " public DataTable Select(string sql) { \r\n ";

                sd_text += " SqlConnection conn = getConnection(); \r\n";

                sd_text += " SqlCommand cmd = null; \r\n";

                sd_text += " DataTable dt = new DataTable(); \r\n";

                sd_text += " try{ \r\n";

                sd_text += "  conn.Open(); \r\n";

                sd_text += " cmd = new SqlCommand(sql, conn); \r\n";

                sd_text += " SqlDataAdapter sda = new SqlDataAdapter(cmd); \r\n";

                sd_text += " sda.Fill(dt); \r\n } \r\n";

                sd_text += " catch (Exception ex) { \r\n ";

                sd_text += "  MessageBox.Show(ex.Message); \r\n } \r\n ";

                sd_text += " try { \r\n "; ;

                sd_text += " conn.Close(); \r\n";

                sd_text += "  cmd.Dispose(); \r\n } \r\n";

                sd_text += " catch (Exception ex) { \r\n";

                sd_text += " Console.WriteLine(ex.Message);} \r\n";

                sd_text += "  return dt; \r\n } \r\n";


                sd_text += " public  void ExecuteSQLTran(string sqlString) { \r\n";

                sd_text += " SqlConnection con = getConnection(); \r\n";

                sd_text += " SqlCommand cmd = new SqlCommand(sqlString, con) \r\n";

                sd_text += " con.Open(); \r\n";

                sd_text += " SqlTransaction tran = con.BeginTransaction(); \r\n";

                sd_text += " cmd.Transaction = tran;  ";

                sd_text += " try { \r\n";

                sd_text += " cmd.ExecuteNonQuery(); \r\n ";

                sd_text += " tran.Commit(); \r\n";

                sd_text += " }  \r\n";

                sd_text += " catch (Exception e) {  \r\n ";

                sd_text += " tran.Rollback(); \r\n  } \r\n";

                sd_text += " finally { \r\n";

                sd_text += " con.Close();";

                sd_text += " } \r\n";


                sd_text += " } \r\n";


                sd_text += " \r\n } \r\n } ";

                byte[] data3 = Encoding.Default.GetBytes(sd_text);
                //开始写入
                fs3.Write(data3, 0, data3.Length);
                //清空缓冲区、关闭流
                fs3.Flush();
                fs3.Close();



            }
            catch
            {

            }

            

        }



        private void Generate_MYSQL(string server, string database, string user, string pass,string namespace_str)
        {

            string ConnectString = string.Format("Server={0};User ID={1};Password={2};Database={3};SslMode = none;", server, user, pass, database);
            MySqlConnection con = new MySqlConnection(ConnectString);
            

            string path = Application.StartupPath + "//entity";

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }


            string path2 = Application.StartupPath + "//dao";

            if (Directory.Exists(path2))
            {
                Directory.Delete(path2, true);
            }


            try
            {

                //select TABLE_NAME,TABLE_TYPE,TABLE_CATALOG,TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES;


                string sql = "select TABLE_NAME from information_schema.TABLES WHERE TABLE_SCHEMA='" + database+"' and  TABLE_TYPE='BASE TABLE';";

                DataTable dt = Select_MYSQL(sql, con);


                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    string tb_name = dt.Rows[i]["TABLE_NAME"].ToString();
                    // Console.WriteLine("表名称:"+tb_name);
                    label7.Text = tb_name;
                    //tb_name=tb_name.ToUpper();
                    string head = "using System.Data; \r\n\r\n\r\n";
                    head += "using System.Collections.Generic;\r\n";
                    head += "namespace " + namespace_str + " \r\n  {\r\n   class " + tb_name.ToUpper() + " \r\n { \r\n";
                    head += "public " + tb_name.ToUpper() + " () \r\n { \r\n }\r\n ";
                    // Console.WriteLine(head);

                    sql = "select DISTINCT(COLUMN_NAME),DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE from INFORMATION_SCHEMA.COLUMNS t where t.TABLE_NAME = '" + tb_name + "';";

                    DataTable dt2 = Select_MYSQL(sql, con);

                    //sql = "SELECT TABLE_NAME,COLUMN_NAME  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='"+tb_name+"'";
                    sql = "SELECT  COLUMN_NAME  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='" + tb_name + "'";

                    DataTable dt3 = Select_MYSQL(sql, con);

                    string pk = "";

                     

                    if (dt3.Rows.Count > 0)
                    {
                        
                         
                        pk = dt3.Rows[0]["COLUMN_NAME"].ToString();
                        
                    }

                    string code_str = "";

                    string fields = "";

                    string sql_insert = "";

                    string sql_update = "";

                    string sql_delete = "";

                   // string save_parsm_list = "";

                    string update_parsm_list = "";

                    string delete_parsm_list = "";




                    sql_insert += "insert into " + tb_name + " ( ";

                    sql_update += "update " + tb_name + " set ";

                    sql_delete += " delete from " + tb_name;

                    string sql_insert_params_key = "";

                    string sql_insert_params_value = "";


                    string sql_update_params_key = "";

                    string sql_update_where = "";



                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {

                        string col_name = dt2.Rows[j]["COLUMN_NAME"].ToString();

                        string col_type = dt2.Rows[j]["DATA_TYPE"].ToString();

                        SqlDbType dbt = SQLServerUtils.SqlTypeString2SqlType(col_type);



                        Type t = SQLServerUtils.SqlType2CsharpType(dbt);



                        code_str += "\t public\t" + t.ToString() + "\t" + col_name.ToLower() + " {get;set;}\r\n";

                        fields += " dict.Add(\"" + col_name.ToLower() + "\", \"" + col_type + "\");\r\n";

                        if (!string.IsNullOrEmpty(pk))
                        {
                            if (col_name.Equals(pk))
                            {
                              //  delete_parsm_list = " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";

                                sql_update_where += "'\"+" + tb_name.ToLower() + "." + col_name.ToLower() + "+\"'";

                            }
                            else
                            {
                                sql_insert_params_key += "" + col_name.ToLower() + ",";

                                sql_insert_params_value += tb_name.ToLower() + "." + col_name.ToLower() + "+\"','\"+";

                                sql_update_params_key += "" + col_name.ToLower() + "='\"+" + tb_name.ToLower() + "." + col_name.ToLower() + "+\"',";

                               // save_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";
                            }
                        }
                        else
                        {
                          //  save_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";
                        }


                        //update_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",SqlDbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";

                        //update_parsm_list += "" + col_name.ToLower() + "='" + col_name.ToLower() + ",";


                        // Console.WriteLine(code_str);


                    }

                    if (!string.IsNullOrEmpty(fields))
                    {
                        fields = fields.Substring(0, fields.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_insert_params_key))
                    {
                        sql_insert_params_key = sql_insert_params_key.Substring(0, sql_insert_params_key.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_insert_params_value))
                    {
                        sql_insert_params_value = sql_insert_params_value.Substring(0, sql_insert_params_value.Length - 4);
                    }

                    if (!string.IsNullOrEmpty(sql_update_params_key))
                    {
                        sql_update_params_key = sql_update_params_key.Substring(0, sql_update_params_key.Length - 1);
                    }



                    sql_insert += sql_insert_params_key + " ) values ('\"+" + sql_insert_params_value + ") ; ";

                    sql_update += sql_update_params_key + " where " + sql_update_where + ";";

                    sql_delete += " where " + sql_update_where;


                    string fields_str = "\r\n public Dictionary<string,string> fields(){\r\n";
                    fields_str += "Dictionary<string, string> dict = new Dictionary<string, string>();\r\n ";
                    fields_str += fields;
                    fields_str += "return dict;";
                    fields_str += "}\r\n";

                    string foot = "\r\n}\r\n}";
                    // Console.WriteLine(foot);

                    string full_text = head + code_str + fields_str + foot;


                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch
                    {

                    }

                    string file = path + "//" + tb_name + ".cs";

                    FileStream fs = new FileStream(file, FileMode.Create);
                    //获得字节数组
                    byte[] data = Encoding.Default.GetBytes(full_text);
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();



                    //创建操作类

                    string param = tb_name.ToLower();

                    full_text = "";

                    head = "using System; \r\n using System.Data; \r\n\r\n\r\n";
                    head += "using System.Data.SqlClient;\r\n";
                    head += "using MySql.Data.MySqlClient;\r\n";
                    head += "namespace " + namespace_str + " \r\n  {\r\n   class " + tb_name.ToUpper() + "_DAO \r\n { \r\n";
                    head += "public " + tb_name.ToUpper() + "_DAO () \r\n { \r\n }\r\n ";

                    full_text += head;

                    full_text += " SuperDAO super = new SuperDAO(); \r\n";

                    full_text += "\r\n public long save(" + tb_name.ToUpper() + " " + param + ")\r\n{\r\n ";

                    full_text += " MySqlConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_insert + "\"; \r\n";

                    full_text += "MySqlCommand cmd = new MySqlCommand(sql, con);\r\n";

                   // full_text += save_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    //full_text += " cmd = new SqlCommand(\"select @@IDENTITY as 'id'\", con); \r\n"; ;

                    full_text += "long id = cmd.LastInsertedId; ;\r\n";
                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";
                    full_text += "return id; \r\n";

                    full_text += "  }\r\n";

                    full_text += "\r\n public void update(" + tb_name.ToUpper() + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " MySqlConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_update + "\"; \r\n";

                    full_text += "MySqlCommand cmd = new MySqlCommand(sql, con);\r\n";

                    full_text += update_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";

                    full_text += "  }\r\n";

                    full_text += "\r\n public void delete(" + tb_name.ToUpper() + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " MySqlConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_delete + "\"; \r\n";

                    full_text += " MySqlCommand cmd = new MySqlCommand(sql, con);\r\n";

                    full_text += delete_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";

                    full_text += "  }\r\n";


                    full_text += "\r\n}\r\n}";


                    try
                    {
                        Directory.CreateDirectory(path2);
                    }
                    catch
                    {

                    }


                    file = path2 + "//" + tb_name + "_DAO.cs";

                    fs = new FileStream(file, FileMode.Create);
                    //获得字节数组
                    data = Encoding.Default.GetBytes(full_text);
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();






                }


                string file3 = path2 + "//SuperDAO.cs";

                FileStream fs3 = new FileStream(file3, FileMode.Create);
                //获得字节数组
                string sd_text = "";

                sd_text += "using System;\r\n";

                sd_text += "using System.Data; \r\n";

                sd_text += "using System.Data.SqlClient; \r\n";

                sd_text += "using MySql.Data.MySqlClient; \r\n";

                sd_text += "using System.Windows.Forms; \r\n";

                sd_text += "namespace " + namespace_str + " { \r\n";

                sd_text += "  class SuperDAO {\r\n ";

                sd_text += "  public SuperDAO() {\r\n  } \r\n";

                sd_text += " public MySqlConnection getConnection() { \r\n ";

                sd_text += " string server = \"" + server + "\"; \r\n";

                sd_text += "  string database = \"" + database + "\"; \r\n";
                sd_text += "string user_id = \"" + user + "\"; \r\n";
                sd_text += "  string password = \"" + pass + "\" ; \r\n";
                

                sd_text += " string ConnectString = string.Format(\"Server ={0}; User ID = {1}; Password ={2}; Database ={3}; SslMode = none; \", server, user_id, password, database);\r\n";
                sd_text += " MySqlConnection conn = new MySqlConnection(ConnectString); \r\n " ;
           

                sd_text += "return conn; \r\n";

                sd_text += " } \r\n";


                sd_text += "public DataTable getData(string tableName, string where) { \r\n";

                sd_text += "  string sql = \"select* from \" + tableName; \r\n";

                sd_text += " if (!string.IsNullOrEmpty(where)) {  \r\n";

                sd_text += " sql += \" where \" + where;  \r\n";

                sd_text += " } \r\n";

                sd_text += "  DataTable dataTable = Select(sql); \r\n";

                sd_text += " return dataTable;  \r\n  } \r\n ";

                sd_text += " public DataTable Select(string sql) { \r\n ";

                sd_text += " MySqlConnection conn = getConnection(); \r\n";

                sd_text += " MySqlCommand cmd = null; \r\n";

                sd_text += " DataTable dt = new DataTable(); \r\n";

                sd_text += " try{ \r\n";

                sd_text += "  conn.Open(); \r\n";

                sd_text += " cmd = new MySqlCommand(sql, conn); \r\n";

                sd_text += " MySqlDataAdapter sda = new MySqlDataAdapter(cmd); \r\n";

                sd_text += " sda.Fill(dt); \r\n } \r\n";

                sd_text += " catch (Exception ex) { \r\n ";

                sd_text += "  MessageBox.Show(ex.Message); \r\n } \r\n ";

                sd_text += " try { \r\n "; ;

                sd_text += " conn.Close(); \r\n";

                sd_text += "  cmd.Dispose(); \r\n } \r\n";

                sd_text += " catch (Exception ex) { \r\n";

                sd_text += " Console.WriteLine(ex.Message);} \r\n";

                sd_text += "  return dt; \r\n } \r\n";

                
                sd_text += " public  void ExecuteSQLTran(string sqlString) { \r\n";

                sd_text += " MySqlConnection con = getConnection(); \r\n";

                sd_text += " MySqlCommand cmd = new MySqlCommand(sqlString, con); \r\n";

                sd_text += " con.Open(); \r\n";

                sd_text += " MySqlTransaction tran = con.BeginTransaction(); \r\n";

                sd_text += " cmd.Transaction = tran;  \r\n ";

                sd_text += " try { \r\n";

                sd_text += " cmd.ExecuteNonQuery(); \r\n ";

                sd_text += " tran.Commit(); \r\n";

                sd_text += " }  \r\n";

                sd_text += " catch (Exception e) {  \r\n ";

                sd_text += " tran.Rollback(); \r\n  } \r\n";

                sd_text += " finally { \r\n";

                sd_text += " con.Close();";

                sd_text += " } \r\n";


                sd_text += " } \r\n";



                sd_text += " \r\n } \r\n } ";

                byte[] data3 = Encoding.Default.GetBytes(sd_text);
                //开始写入
                fs3.Write(data3, 0, data3.Length);
                //清空缓冲区、关闭流
                fs3.Flush();
                fs3.Close();



            }
            catch
            {

            }



        }

        private void Generate_SQLITE(string server, string database,string pass, string namespace_str)
        {

            string p = "";
            if (!string.IsNullOrEmpty(pass))
            {
                p = "Password=" + pass;
            }

            string ConnectionString = "Data Source=" + server + ";Version=3;" + p;

            SQLiteConnection con = new SQLiteConnection(ConnectionString);

            string path = Application.StartupPath + "//entity";

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }


            string path2 = Application.StartupPath + "//dao";

            if (Directory.Exists(path2))
            {
                Directory.Delete(path2, true);
            }


            try
            {

                //select TABLE_NAME,TABLE_TYPE,TABLE_CATALOG,TABLE_SCHEMA from INFORMATION_SCHEMA.TABLES;


                string sql = "";

                con.Open();

                DataTable dt = con.GetSchema("TABLES");

                con.Close();

                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    string tb_name = dt.Rows[i]["TABLE_NAME"].ToString();

                    string tb_type = dt.Rows[i]["TABLE_TYPE"].ToString();

                    if (tb_type.Equals("SYSTEM_TABLE"))
                    {
                        continue;
                    }

                    // Console.WriteLine("表名称:"+tb_name);
                    label7.Text = tb_name;
                    string head = "using System.Data; \r\n\r\n\r\n";
                    head += "using System.Data.SQLite; \r\n";
                    head += "using System.Collections.Generic;\r\n";
                    head += "namespace " + namespace_str + " \r\n  {\r\n   class " + tb_name.ToUpper() + " \r\n { \r\n";
                    head += "public " + tb_name.ToUpper() + " () \r\n { \r\n }\r\n ";
                    // Console.WriteLine(head);

                    sql = "PRAGMA table_info ('" + tb_name + "') ;";

                    DataTable dt2 =Select_SQLITE(sql, con);

                    

                    string pk = "";

                    if (dt2.Rows.Count > 0)
                    {
                        DataRow[] r = dt2.Select("pk='1'");
                        if (r.Length > 0)
                        {
                            pk = r[0]["name"].ToString();
                        }
                    }

                    string code_str = "";

                    string fields = "";

                    string sql_insert = "";

                    string sql_update = "";

                    string sql_delete = "";

                    string save_parsm_list = "";

                    string update_parsm_list = "";

                    string delete_parsm_list = "";




                    sql_insert += "insert into " + tb_name + " ( ";

                    sql_update += "update " + tb_name + " set ";

                    sql_delete += " delete from " + tb_name;

                    string sql_insert_params_key = "";

                    string sql_insert_params_value = "";


                    string sql_update_params_key = "";

                    string sql_update_where = "";



                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {

                        string col_name = dt2.Rows[j]["name"].ToString();

                        string col_type = dt2.Rows[j]["type"].ToString();

                        Type t = SQLITEUtils.SQLITE_type(col_type);

                        DbType dbt = SQLITEUtils.db_type(col_type);

                        code_str += "\t public\t" + t.ToString() + "\t" + col_name.ToLower() + " {get;set;}\r\n";

                        fields += " dict.Add(\"" + col_name.ToLower() + "\", \"" + col_type + "\");\r\n";

                        if (!string.IsNullOrEmpty(pk))
                        {
                            if (col_name.Equals(pk))
                            {
                                delete_parsm_list = " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",DbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";

                                sql_update_where += col_name.ToLower() + "=@" + col_name.ToLower();

                            }
                            else
                            {
                                sql_insert_params_key += "" + col_name.ToLower() + ",";
                                sql_insert_params_value += "@" + col_name.ToLower() + ",";

                                sql_update_params_key += "" + col_name.ToLower() + "=@" + col_name.ToLower() + ",";

                                save_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",DbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";
                            }
                        }
                        else
                        {
                            save_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",DbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";
                        }


                       update_parsm_list += " cmd.Parameters.Add(\"@" + col_name.ToLower() + "\",DbType." + dbt.ToString() + ").Value=" + tb_name.ToLower() + "." + col_name.ToLower() + "; \r\n";



                        // Console.WriteLine(code_str);


                    }

                    if (!string.IsNullOrEmpty(fields))
                    {
                        fields = fields.Substring(0, fields.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_insert_params_key))
                    {
                        sql_insert_params_key = sql_insert_params_key.Substring(0, sql_insert_params_key.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_insert_params_value))
                    {
                        sql_insert_params_value = sql_insert_params_value.Substring(0, sql_insert_params_value.Length - 1);
                    }

                    if (!string.IsNullOrEmpty(sql_update_params_key))
                    {
                        sql_update_params_key = sql_update_params_key.Substring(0, sql_update_params_key.Length - 1);
                    }



                    sql_insert += sql_insert_params_key + " ) values (" + sql_insert_params_value + ") ; ";

                    sql_update += sql_update_params_key + " where " + sql_update_where + ";";

                    sql_delete += " where " + sql_update_where;


                    string fields_str = "\r\n public Dictionary<string,string> fields(){\r\n";
                    fields_str += "Dictionary<string, string> dict = new Dictionary<string, string>();\r\n ";
                    fields_str += fields;
                    fields_str += "return dict;";
                    fields_str += "}\r\n";

                    string foot = "\r\n}\r\n}";
                    // Console.WriteLine(foot);

                    string full_text = head + code_str + fields_str + foot;


                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch
                    {

                    }

                    string file = path + "//" + tb_name.ToUpper() + ".cs";

                    FileStream fs = new FileStream(file, FileMode.Create);
                    //获得字节数组
                    byte[] data = Encoding.Default.GetBytes(full_text);
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();



                    //创建操作类

                    

                    full_text = "";

                    head = "using System; \r\n using System.Data; \r\n\r\n\r\n";
                    head += "using System.Data.SQLite;\r\n";
                    head += "namespace " + namespace_str + " \r\n  {\r\n   class " + tb_name.ToUpper() + "_DAO \r\n { \r\n";
                    head += "public " + tb_name.ToUpper() + "_DAO () \r\n { \r\n }\r\n ";

                    full_text += head;

                    full_text += " SuperDAO super = new SuperDAO(); \r\n";

                    full_text += "\r\n public int save(" + tb_name.ToUpper() + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " SQLiteConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_insert + "\"; \r\n";

                    full_text += "SQLiteCommand cmd = new SQLiteCommand(sql, con);\r\n";

                    full_text += save_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += " cmd = new SQLiteCommand(\"select @@IDENTITY as 'id'\", con); \r\n"; ;
                    full_text += "int id = Convert.ToInt32(cmd.ExecuteScalar());\r\n";
                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";
                    full_text += "return id; \r\n";

                    full_text += "  }\r\n";

                    full_text += "\r\n public void update(" + tb_name.ToUpper() + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " SQLiteConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_update + "\"; \r\n";

                    full_text += "SQLiteCommand cmd = new SQLiteCommand(sql, con);\r\n";

                    full_text += update_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";

                    full_text += "  }\r\n";

                    full_text += "\r\n public void delete(" + tb_name.ToUpper() + " " + tb_name.ToLower() + ")\r\n{\r\n ";

                    full_text += " SQLiteConnection con = super.getConnection();\r\n";

                    full_text += "  con.Open(); \r\n";

                    full_text += " string sql = \"" + sql_delete + "\"; \r\n";

                    full_text += "SQLiteCommand cmd = new SQLiteCommand(sql, con);\r\n";

                    full_text += delete_parsm_list;

                    full_text += " cmd.ExecuteNonQuery();\r\n";

                    full_text += "con.Close();\r\n";
                    full_text += "con.Dispose();\r\n";
                    full_text += "cmd = null; \r\n";

                    full_text += "  }\r\n";


                    full_text += "\r\n}\r\n}";


                    try
                    {
                        Directory.CreateDirectory(path2);
                    }
                    catch
                    {

                    }


                    file = path2 + "//" + tb_name.ToUpper() + "_DAO.cs";

                    fs = new FileStream(file, FileMode.Create);
                    //获得字节数组
                    data = Encoding.Default.GetBytes(full_text);
                    //开始写入
                    fs.Write(data, 0, data.Length);
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();






                }


                string file3 = path2 + "//SuperDAO.cs";

                FileStream fs3 = new FileStream(file3, FileMode.Create);
                //获得字节数组
                string sd_text = "";

                sd_text += "using System;\r\n";

                sd_text += "using System.Data; \r\n";

                sd_text += "using System.Data.SQLite; \r\n";

                sd_text += "using System.Windows.Forms; \r\n";

                sd_text += "namespace " + namespace_str + " { \r\n";

                sd_text += "  class SuperDAO {\r\n ";

                sd_text += "  public SuperDAO() {\r\n  } \r\n";

                sd_text += " public  SQLiteConnection getConnection() { \r\n ";

                sd_text += " string server = \"" + server.Replace('\\','/') + "\"; \r\n";

                sd_text += "  string database = \"" + database + "\"; \r\n";

                if (!string.IsNullOrEmpty(pass))
                {
                    sd_text += "  string password = \"" + pass + "\" ; \r\n";
                }

                

                if (!string.IsNullOrEmpty(pass))
                {
                    sd_text += " string connectionStringTest = @\"Data Source = \" + server + \"; database = \" + database + \";Version=3;  password = \" + password + \";  \"; \r\n";
                }
                else
                {
                    sd_text += " string connectionStringTest = @\"Data Source =  \" + server + \"; database = \" + database + \"; Version=3; \"; \r\n";
                }
                sd_text += "return new SQLiteConnection(connectionStringTest); \r\n";

                sd_text += " } \r\n";


                sd_text += "public DataTable getData(string tableName, string where) { \r\n";

                sd_text += "  string sql = \"select* from \" + tableName; \r\n";

                sd_text += " if (!string.IsNullOrEmpty(where)) {  \r\n";

                sd_text += " sql += \" where \" + where;  \r\n";

                sd_text += " } \r\n";

                sd_text += "  DataTable dataTable = Select(sql); \r\n";

                sd_text += " return dataTable;  \r\n  } \r\n ";

                sd_text += " public DataTable Select(string sql) { \r\n ";

                sd_text += " SQLiteConnection conn = getConnection(); \r\n";

                sd_text += " SQLiteCommand cmd = null; \r\n";

                sd_text += " DataTable dt = new DataTable(); \r\n";

                sd_text += " try{ \r\n";

                sd_text += "  conn.Open(); \r\n";

                sd_text += " cmd = new SQLiteCommand(sql, conn); \r\n";

                sd_text += " SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd); \r\n";

                sd_text += " sda.Fill(dt); \r\n } \r\n";

                sd_text += " catch (Exception ex) { \r\n ";

                sd_text += "  MessageBox.Show(ex.Message); \r\n } \r\n ";

                sd_text += " try { \r\n "; ;

                sd_text += " conn.Close(); \r\n";

                sd_text += "  cmd.Dispose(); \r\n } \r\n";

                sd_text += " catch (Exception ex) { \r\n";

                sd_text += " Console.WriteLine(ex.Message);} \r\n";

                sd_text += "  return dt; \r\n } \r\n";

                sd_text += " public  void ExecuteSQLTran(string sqlString) { \r\n";

                sd_text += " SQLiteConnection con = getConnection(); \r\n";

                sd_text += " SQLiteCommand cmd = new SQLiteCommand(sqlString, con) \r\n";

                sd_text += " con.Open(); \r\n";

                sd_text += " SQLiteTransaction tran = con.BeginTransaction(); \r\n";

                sd_text += " cmd.Transaction = tran;  ";

                sd_text += " try { \r\n";

                sd_text += " cmd.ExecuteNonQuery(); \r\n ";

                sd_text += " tran.Commit(); \r\n";

                sd_text += " }  \r\n";

                sd_text += " catch (Exception e) {  \r\n ";

                sd_text += " tran.Rollback(); \r\n  } \r\n";

                sd_text += " finally { \r\n";

                sd_text += " con.Close();";

                sd_text += " } \r\n";


                sd_text += " } \r\n";



                sd_text += " \r\n } \r\n } ";

                byte[] data3 = Encoding.Default.GetBytes(sd_text);
                //开始写入
                fs3.Write(data3, 0, data3.Length);
                //清空缓冲区、关闭流
                fs3.Flush();
                fs3.Close();



            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }








        public DataTable Select(string sql,SqlConnection conn)
        {
            SqlCommand cmd = null;
            DataTable dt = new DataTable();
            try
            {
                conn.Open();
                cmd = new SqlCommand(sql, conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);

                sda.Fill(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            try
            {
                conn.Close();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return dt;
        }


        public DataTable Select_MYSQL(string sql, MySqlConnection conn)
        {
            MySqlCommand cmd = null;
            DataTable dt = new DataTable();
            try
            {
                conn.Open();
                cmd = new MySqlCommand(sql, conn);

                MySqlDataAdapter  sda = new MySqlDataAdapter(cmd);

                sda.Fill(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            try
            {
                conn.Close();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return dt;
        }



        public DataTable Select_SQLITE(string sql, SQLiteConnection conn)
        {
            SQLiteCommand cmd = null;
            DataTable dt = new DataTable();
            try
            {
                conn.Open();
                cmd = new SQLiteCommand(sql, conn);

                 SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);

                sda.Fill(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            try
            {
                conn.Close();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return dt;
        }



        private void generate(string str)
        {

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "txt(*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 0;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.CreatePrompt = true;
            saveFileDialog1.Title = "导出txt文件到 ";

            DateTime now = DateTime.Now;
            saveFileDialog1.FileName = now.Second.ToString().PadLeft(2, '0');
            //now.Year.ToString().PadLeft(2)+now.Month.ToString().PadLeft(2, '0 ') +now.Day.ToString().PadLeft(2, '0 ')+ "_ " +now.Hour.ToString().PadLeft(2, '0 ') +now.Minute.ToString().PadLeft(2, '0 ') +
            saveFileDialog1.ShowDialog();

            Stream myStream;
            myStream = saveFileDialog1.OpenFile();
            //StreamWriter   sw=new   StreamWriter(myStream,System.Text.Encoding.GetEncoding( "gb2312 "));
            //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(Encoding.UTF8.ToString()));
            StreamWriter sw = new StreamWriter(myStream, Encoding.UTF8);

           
            sw.WriteLine(str);
            //写内容

            sw.Close();
            myStream.Close();



        }


        private SqlConnection getConnection(string connectionStringTest)
        {

        

            SqlConnection conn = new SqlConnection(connectionStringTest);

            return conn;


        }

        private void Form1_Load(object sender, EventArgs e)
        {



            // mysql_insert();

           // sqlite_select();
        }

        private void mysql_insert()
        {
          // WORD w = new WORD();
          // w.title = "A";
          // w.date = DateTime.Now;
          // w.content = "this is test content";
          // w.author = "me";
          // WORD_DAO wd = new WORD_DAO();
          // long id=wd.save(w);
          //
          // MessageBox.Show("Mysql insert success id:"+id.ToString());
        }
        private void sqlite_select()
        {
            DATABASE_DAO dbd = new DATABASE_DAO();
            DATABASE db = new DATABASE();
            db.name = "DawayiSY";
            dbd.save(db);
            DataTable dt=dbd.select(db);
            Console.WriteLine(dt.Rows.Count.ToString());

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index=tabControl1.SelectedIndex;
            switch (index)
            {
                case 0:
                    textBox4.Text = "Connection Timeout=10;";
                    button3.Visible = false;
                    break;
                case 1:
                    textBox4.Text = "";
                    button3.Visible = false;
                    break;

                case 2:
                    textBox2.Text = "";
                    textBox3.Text = "";
                    button3.Visible = true;
                    textBox4.Text = "";
                    break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = " 选择文件";
            fileDialog.Filter = "*.db;*.sqlite;*.sqlite3;|*.db;*.sqlite;*.sqlite3";
            string file = "";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                file = fileDialog.FileName;
                textBox1.Text = file;



            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4();
            form4.ShowDialog();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Form6 form6 = new Form6();
            form6.ShowDialog();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Form7 form7 = new Form7();
            form7.ShowDialog();
        }
    }
}
